/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-02-06
 * Time: 20:57
 */
public class MySingleList {
    //内部类
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    //
    private ListNode head;
    //创建链表
    public void createList(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }
    //打印链表
    public void disPlay(){
        ListNode cur = head;
        if (head == null){
            System.out.println("链表为空");
            return;
        }
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //求链表长度
    public int getSize(){
        int count = 0;
        ListNode cur = head;
        while (cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    //查找key
    public Boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if (cur == null){
            head = node;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //在pos位置添加元素,第一个节点下标为0,假如输入pos=2,那么该节点插入后就是链表的第2个节点
    public void addPos(int pos,int data){
        if (pos < 0 || pos > getSize()){
            System.out.println("插入位置不合法");
            return;
        }
        //pos == 0,直接使用头插法
        if(pos == 0){
            addFirst(data);
            return;
        }
        //pos == getSize(),使用尾插法
        if (pos == getSize()){
            addLast(data);
            return;
        }
        ListNode cur = toFindAddPosSubOne(pos);
        ListNode node = new ListNode(data);
        if (cur != null) {
            node.next = cur.next;
            cur.next = node;
        }
    }
    //第一个节点下标为0,找到pos下标的前一个节点,并且返回该节点的地址
    private ListNode toFindAddPosSubOne(int pos){
        if (pos < 0 || pos > getSize()){
            System.out.println("输入的位置不合法");
            return null;
        }
        ListNode cur = head;
        while ( pos - 1 != 0){
            cur = cur.next;
            pos--;
        }
        return cur;
    }
    //删除出现的第一个key目标
    public void delFirstKey(int key){
        if(head == null){
            System.out.println("链表为空");
            return;
        }
        if (head.val == key){
            head = head.next;
        }
        ListNode cur = toFindDel(key);
        if (cur == null){
            System.out.println("没有你要删除的数字");
            return;
        }
        cur.next = cur.next.next;
    }
    //找到要删除节点的前一个节点
    private ListNode toFindDel(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除出现的所有key目标
    public void delAllKey(int key){
        if(head == null){
            System.out.println("链表为空");
            return;
        }
        while (head.val == key){
            head = head.next;
        }
        ListNode cur = head;
        ListNode del = cur.next;
        int count = 0;
        while (del != null){
            if (del.val != key){
                del = del.next;
                cur = cur.next;
            }else {
                cur.next = del.next;
                del = del.next;
                count++;
            }
        }
        if (count == 0){
            System.out.println("没有你要删除的数字");
        }
    }
    //清空链表
    public void clear(){
        head = null;
    }
    //逆置
    private ListNode reverseList(){
        if(head == null){
            System.out.println("链表为空");
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    //
    private void disPlay(ListNode ret) {
        ListNode cur = ret;
        if (ret == null) {
            System.out.println("链表为空");
            return;
        }
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //找到中间节点
    private ListNode middleList (){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    //找到倒数第k个节点
    private ListNode findToTail(int k){
        if (k <= 0 || head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k-1; i++) {
            fast = fast.next;
            if (fast == null){
                return null;
            }
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //合并两个有序链表main2
    private static ListNode mergeTwoLists(ListNode headA,ListNode headB){
        ListNode newHead = new ListNode(-1);
        ListNode tempHead = newHead;
        while(headA != null && headB != null){
            if (headA.val > headB.val){
                tempHead.next = headB;
                headB = headB.next;
            }else {
                tempHead.next = headA;
                headA = headA.next;
            }
            tempHead = tempHead.next;
        }
        if (headA != null){
            tempHead.next = headA;
        }
        if (headB != null){
            tempHead.next = headB;
        }
        return newHead.next;
    }
    //给定一个数字，小的在左边，大的在右边，不改变原来的顺序main
    private ListNode severFromData(int data){
        ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;
        ListNode cur = head;
        while (cur != null){
            if (cur.val > data){
                if (as == null){
                    as = ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }else {
                if (bs == null){
                    bs = be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null){
            return as;
        }
        be.next = as;
        if (as != null){
            ae.next = null;
        }
        return bs;
    }
    //回文
    public boolean ifSame(){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        while (head != slow){
            if (head.val != slow.val){
                return false;
            }
            if (head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }



    public static void main2(String[] args) {

        MySingleList mySingleList1 =new MySingleList();
        mySingleList1.addLast(12);
        mySingleList1.addLast(22);
        mySingleList1.addLast(32);
        mySingleList1.addLast(42);
        mySingleList1.addLast(92);
        mySingleList1.disPlay();

        MySingleList mySingleList2 =new MySingleList();
        mySingleList2.addLast(2);
        mySingleList2.addLast(45);
        mySingleList2.addLast(53);
        mySingleList2.addLast(87);
        mySingleList2.addLast(134);
        mySingleList2.disPlay();

        ListNode ret =  mergeTwoLists(mySingleList1.head,mySingleList2.head);
        mySingleList1.disPlay(ret);
    }



    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        //创建链表
        //mySingleList.createList();
        //头插法
        mySingleList.addFirst(50);
        mySingleList.addFirst(40);
        mySingleList.addFirst(30);
        mySingleList.addFirst(20);
        mySingleList.addFirst(10);
        mySingleList.disPlay();
        System.out.println("-------------------------------------");
        //尾插法
        mySingleList.addLast(60);
        mySingleList.disPlay();
        System.out.println("-------------------------------------");
        //选择插入
        mySingleList.addPos(3,30);
        mySingleList.disPlay();
        System.out.println("-------------------------------------");
        //
//        mySingleList.delFirstKey(30);
//        mySingleList.disPlay();
//        System.out.println("------------------------------------");
        //
        mySingleList.delAllKey(300);
        mySingleList.disPlay();
        System.out.println("-------------------------------------");
        //
        mySingleList.reverseList();
        mySingleList.disPlay();
        System.out.println("-------------------------------------");
        //
        ListNode ret1 = mySingleList.middleList();
        System.out.println(ret1.val);
        System.out.println("-------------------------------------");
        //找到倒数第k个节点
        ListNode ret2 = mySingleList.findToTail(1);
        System.out.println(ret2.val);
        System.out.println("-------------------------------------");
        ListNode ret3 =  mySingleList.severFromData(23);
        mySingleList.disPlay(ret3);
        System.out.println("-------------------------------------");
        Boolean ret4 =  mySingleList.ifSame();
        System.out.println(ret4);
        System.out.println("-------------------------------------");
//        ListNode ret = mySingleList.reverseList();
//        mySingleList.disPlay(ret);
        //清空
//        mySingleList.clear();
//        mySingleList.disPlay();
//        System.out.println("-------------------------------------");
        //长度
//        System.out.println(mySingleList.getSize());
//        System.out.println("-------------------------------------");
        //查找
//        System.out.println(mySingleList.contains(3));
//        System.out.println("-------------------------------------");
    }
}
